package com.redis.controller;

import com.redis.annotation.Limit;
import com.redis.lock.lock.CacheLock;
import com.redis.lock.lock.CacheParam;
import com.redis.service.RedisServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author huyunqiang
 * @Date 2019/11/12 10:13
 */
@RestController
@RequestMapping("/redisLimit")
public class LockRedisController {

    @Autowired
    RedisServer redisServer;

    private static final AtomicInteger ATOMIC_INTEGER = new AtomicInteger();

    /**
     * 测试分布式限流
     * @return
     */
    @Limit(key = "test", period = 10, count = 3)
    @GetMapping("/test")
    public int testLimiter() {
        System.out.println("来了啊");
        // 意味著 100S 内最多允許訪問10次
        return ATOMIC_INTEGER.incrementAndGet();
    }

    /**
     * 测试分布式限流非注解
     * @return
     */
    @GetMapping("/test2")
    public void redisLimit(){

        boolean b = redisServer.getRedisLimit("nihao",60,3);
        if(b){
            System.out.println("来了啊");
        }else{
            System.out.println("xianzhi");
        }


    }

     /**
     * 测试分布式锁 防止重复提交
     * @param token
     * @return
     */
    @CacheLock(prefix = "books")
    @GetMapping("/books")
    public String query(@CacheParam(name = "token") @RequestParam String token) {
        return "success - " + token;
    }



}
